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 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
let board =
    [
        [-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ]
async function main() {
    let killed = kill(0, 2, null, true, null, null, null, null);
    console.log('killed = ', killed);
}

function kill(x, y, dead, first, life, checked, tempIsBlack, array) {
    //遍历过的不要重复
    if (tracks?.[x]?.[y] === undefined|| tracks[x][y] !== 0) {
        return dead;
    } else {
        tracks[x][y] = 1;
    }
    if(board[x] == undefined || board[x][y] == undefined) {
        return dead;
    }
    dead = dead || {
        w: {
            isKilled: false,
            dead: [],
        },
        a: {
            isKilled: false,
            dead: [],
        },
        s: {
            isKilled: false,
            dead: [],
        },
        d: {
            isKilled: false,
            dead: [],
        },
    };
    let isBlack = tempIsBlack || ifBlack;
    //四个方向
    let up = {
        x,
        y: y++,
    };
    let down = {
        x,
        y: y--,
    };
    let left = {
        x: x--,
        y,
    };
    let right = {
        x: x++,
        y,
    };
    console.log('left = ', left)
    console.log('right = ', right);
    console.log('board[x][y] :>> ', board[x][y]);
    // 有气就不算死棋 没有气则直接把自己放入死字数组 开始遍历相连的黑棋
    if (first) {
        //去遍历各个位置的棋
        if (board?.[up.x]?.[up.y] === isBlack ? -1 : 1) {
            dead = concatKill(dead,kill(up.x, up.y, dead, false, null, "w", true, "w"));
        }
        if (board?.[down.x]?.[down.y] === isBlack ? -1 : 1) {
            dead = concatKill(dead,kill(down.x, down.y, dead, false, null, "a", true, "a"));
        }
        if (board?.[left.x]?.[left.y] === isBlack ? -1 : 1) {
            dead = concatKill(dead,kill(left.x, left.y, dead, false, null, "s", true, "s"));
        }
        if (board?.[right.x]?.[right.y] === isBlack ? -1 : 1) {
            dead = concatKill(dead,kill(right.x, right.y, dead, false, null, "d", true, "d"));
        }
    } else {
        //第二次遍历了
        if (checked === "w") {
            //周围一圈没气 这个棋就是死棋
            if (
                board?.[up.x]?.[up.y] !== 0 &&
                board?.[left.x]?.[left.y] !== 0 &&
                board?.[right.x]?.[right.y] !== 0
            ) {
                dead[array].dead.push({ x, y });
                dead[array].isKilled = true;
            } else {
                dead[array].isKilled = false;
                if ((board?.[up.x]?.[up.y] == isBlack) === true ? -1 : 1) {
                    dead = concatKill(dead,kill(up.x, up.y, dead, false, null, "w", null,array));
                }
                if ((board?.[left.x]?.[left.y] == isBlack) === true ? -1 : 1) {
                    dead = concatKill(dead,kill(left.x, left.y, dead, false, null, "a", null,array));
                }
                if ((board?.[right.x]?.[right.y] == isBlack) === true ? -1 : 1) {
                    dead = concatKill(dead,kill(right.x, right.y, dead, false, null, "d", null,array));
                }
            }
        } else if (checked === "a") {
            if (
                board?.[up.x]?.[up.y] !== 0 &&
                board?.[down.x]?.[down.y] !== 0 &&
                board?.[left.x]?.[left.y] !== 0
            ) {
                dead[array].dead.push({ x, y });
                dead[array].isKilled = true;
            } else {
                dead[array].isKilled = false;
                if ((board?.[up.x]?.[up.y] == isBlack) === true ? -1 : 1) {
                    dead = concatKill(dead,kill(up.x, up.y, dead, false, null, "u", null,array));
                }
                if ((board?.[left.x]?.[left.y] == isBlack) === true ? -1 : 1) {
                    dead = concatKill(dead,kill(left.x, left.y, dead, false, null, "a", null,array));
                }
                if ((board?.[down.x]?.[down.y] == isBlack) === true ? -1 : 1) {
                    dead = concatKill(dead,kill(down.x, down.y, dead, false, null, "s", null,array));
                }
            }
        } else if (checked === "s") {
            if (
                board?.[down.x]?.[down.y] !== 0 &&
                board?.[left.x]?.[left.y] !== 0 &&
                board?.[right.x]?.[right.y] !== 0
            ) {
                dead[array].isKilled = true;
                dead[array].dead.push({ x, y });
            } else {
                console.log('dead :>> ', dead);
                console.log('array :>> ', array);
                dead[array].isKilled = false;
                if ((board?.[left.x]?.[left.y] === isBlack) ? -1 : 1) {
                    dead = concatKill(dead,kill(left.x, left.y, dead, false, null, "a", null,array));
                }
                if ((board?.[down.x]?.[down.y] === isBlack) ? -1 : 1) {
                    dead = concatKill(dead,kill(down.x, down.y, dead, false, null, "s", null,array));
                }
                if ((board?.[right.x]?.[right.y] === isBlack) ? -1 : 1) {
                    dead = concatKill(dead,kill(right.x, right.y, dead, false, null, "d", null,array));
                }
            }
        } else if (checked === "d") {
            if (
                board?.[up.x]?.[up.y] !== 0 &&
                board?.[down.x]?.[down.y] !== 0 &&
                board?.[right.x]?.[right.y] !== 0
            ) {
                dead[array].dead.push({ x, y });
                dead[array].isKilled = true;
            } else {
                dead[array].isKilled = false;
                if ((board?.[up.x]?.[up.y] === isBlack) ? -1 : 1) {
                    dead = concatKill(dead,kill(up.x, up.y, dead, false, null, "w", null,array));
                }
                if ((board?.[down.x]?.[down.y] === isBlack) ? -1 : 1) {
                    dead = concatKill(dead,kill(down.x, down.y, dead, false, null, "s", null,array));
                }
                if ((board?.[right.x]?.[right.y] === isBlack) ? -1 : 1) {
                    dead = concatKill(dead,kill(right.x, right.y, dead, false, null, "a", null,array));
                }
            }
        }
        console.log('lastdead :>> ', dead);
        return dead;
    }
}

function concatKill(srcKill,destKill) {
    return{
        w: {
            isKilled: srcKill.w.isKilled,
            dead: srcKill.w.dead.concat(destKill.w.dead),
        },
        a: {
            isKilled: srcKill.a.isKilled,
            dead: srcKill.a.dead.concat(destKill.a.dead),
        },
        s: {
            isKilled: srcKill.s.isKilled,
            dead: srcKill.s.dead.concat(destKill.s.dead),
        },
        d: {
            isKilled: srcKill.d.isKilled,
            dead: srcKill.d.dead.concat(destKill.d.dead),
            dead: [],
        },
    }
}

main();
